package com.framework.loippi.controller.api;

import com.framework.loippi.api.utils.ApiUtils;
import com.framework.loippi.entity.ImUser;
import com.framework.loippi.entity.TUser;
import com.framework.loippi.entity.UserShare;
import com.framework.loippi.service.ImUserService;
import com.framework.loippi.service.TUserService;
import com.framework.loippi.service.UserShareService;
import com.qiniu.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName TUserController
 * @Description TODO
 * @Author fsociety
 * @Date 9/12/2019 下午3:05
 * @Version 1.0
 */
@Controller("apiTUserController")
@RequestMapping("/api/t_user")
@Api(tags = {"用户数据"})
public class TUserController extends ApiBaseController {

    @Resource
    private TUserService tUserService;

    @Resource
    private UserShareService userShareService;

    @Resource
    private ImUserService imUserService;

    @RequestMapping(value = "/bindPhoneById.json", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "根据id绑定手机号", notes = "{}")
    public @ResponseBody
    String bindPhone (@RequestBody JSONObject json) {
        TUser tUser = (TUser) JSONObject .toBean(json, TUser.class);
        Long result = tUserService.update(tUser);

        return ApiUtils.success(result);
    }

    @RequestMapping(value = "/findPhoneById.json", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "根据id查询手机号码", notes = "{}")
    public @ResponseBody
    String findPhoneById (@Param (value = "id") Long id) {
        TUser tUser = tUserService.find(id);

        return ApiUtils.success(tUser);
    }

    @RequestMapping(value = "/share.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ApiOperation("分享")
    public @ResponseBody
    String share(Long userId) {
        Map<String, Object> map = new HashMap();
        UserShare userShare = new UserShare();
        userShare.setUserId(userId);
        userShare.setCreateTime(new Date());
        userShare.setStatus(0);
        userShareService.save(userShare);
        map.put("shareId", userShare.getId());
        return ApiUtils.success(map);
    }

    @RequestMapping(value = "/shareClick.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ApiOperation("分享点击")
    public @ResponseBody
    String shareClick(Long shareId, Long userId, String nickName) {
        UserShare userShare = userShareService.find(shareId);
        Long clickNum = userShare.getClickNum();
        if (clickNum != null) {
            userShare.setClickNum(clickNum++);
        }
        userShare.setClickNum(0L);
        userShare.setStatus(1);
        userShare.setClickedUserId(userId);
        userShare.setClickedNickName(nickName);
        userShare.setUpdateTime(new Date());
        userShareService.update(userShare);
        TUser shareUser = tUserService.find(userId);
        if (shareUser != null) {
            shareUser.setShareUserId(userShare.getUserId());
            tUserService.update(shareUser);
        }

        return ApiUtils.success();
    }

    @RequestMapping(value = "/findShareByUserId.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ApiOperation("根据用户id查询分享")
    public @ResponseBody
    String findShareByUserId(Long userId) {
        List<UserShare> shares = userShareService.findByUserId(userId);
        return ApiUtils.success(shares);
    }


    @RequestMapping(value = "/generatorUserSign.json", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ApiOperation("根据userId生成userSign")
    public @ResponseBody
    String generatorUserSign(String userId) {
        Map<String, Object> map = new HashMap<>();
        if (!StringUtils.isNullOrEmpty(userId)) {
            ImUser imUser = imUserService.find(Long.valueOf(userId));
            if (!StringUtils.isNullOrEmpty(imUser.getUserSign())) {
                map.put("userId", userId);
                map.put("userSign", imUser.getUserSign());
                return ApiUtils.success(map);
            }
            return ApiUtils.error("该用户没有注册到im");
        }
        return null;
    }
}
